package com.free.semantic.infrastruct.repository.impl;

import com.free.semantic.infrastruct.common.UserSemanticNetworkDO;
import com.free.semantic.infrastruct.mapper.UserSemanticNetworkMapper;
import com.free.semantic.infrastruct.repository.UserSemanticNetworkRepository;
import com.free.semantic.model.conceptmodel.UserSemanticNetwork;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class UserSemanticNetworkRepositoryImpl implements UserSemanticNetworkRepository {

    @Autowired
    private UserSemanticNetworkMapper userSemanticNetworkMapper;

    @Override
    public Long create(UserSemanticNetwork userSemanticNetwork) {
        UserSemanticNetworkDO userSemanticNetworkDO = convertToDO(userSemanticNetwork);
        LocalDateTime now = LocalDateTime.now();
        userSemanticNetworkDO.setCreateTime(now);
        userSemanticNetworkDO.setUpdateTime(now);
        userSemanticNetworkMapper.insert(userSemanticNetworkDO);
        return userSemanticNetworkDO.getId();
    }

    @Override
    public void delete(Long id) {
        userSemanticNetworkMapper.deleteById(id);
    }

    @Override
    public UserSemanticNetwork findById(Long id) {
        UserSemanticNetworkDO userSemanticNetworkDO = userSemanticNetworkMapper.findById(id);
        return convertToEntity(userSemanticNetworkDO);
    }

    @Override
    public List<UserSemanticNetwork> findByUserId(Long userId) {
        List<UserSemanticNetworkDO> userSemanticNetworkDOs = userSemanticNetworkMapper.findByUserId(userId);
        return userSemanticNetworkDOs.stream()
                .map(this::convertToEntity)
                .collect(Collectors.toList());
    }

    @Override
    public List<UserSemanticNetwork> findBySemanticNetworkId(Long semanticNetworkId) {
        List<UserSemanticNetworkDO> userSemanticNetworkDOs = userSemanticNetworkMapper.findBySemanticNetworkId(semanticNetworkId);
        return userSemanticNetworkDOs.stream()
                .map(this::convertToEntity)
                .collect(Collectors.toList());
    }

    @Override
    public UserSemanticNetwork findByUserIdAndSemanticNetworkId(Long userId, Long semanticNetworkId) {
        UserSemanticNetworkDO userSemanticNetworkDO = userSemanticNetworkMapper.findByUserIdAndSemanticNetworkId(userId, semanticNetworkId);
        return convertToEntity(userSemanticNetworkDO);
    }

    @Override
    public void updatePermission(UserSemanticNetwork userSemanticNetwork) {
        UserSemanticNetworkDO userSemanticNetworkDO = convertToDO(userSemanticNetwork);
        userSemanticNetworkDO.setUpdateTime(LocalDateTime.now());
        userSemanticNetworkMapper.updatePermission(userSemanticNetworkDO);
    }

    private UserSemanticNetwork convertToEntity(UserSemanticNetworkDO userSemanticNetworkDO) {
        if (userSemanticNetworkDO == null) {
            return null;
        }
        UserSemanticNetwork userSemanticNetwork = new UserSemanticNetwork();
        userSemanticNetwork.setId(userSemanticNetworkDO.getId());
        userSemanticNetwork.setUserId(userSemanticNetworkDO.getUserId());
        userSemanticNetwork.setSemanticNetworkId(userSemanticNetworkDO.getSemanticNetworkId());
        userSemanticNetwork.setPermission(userSemanticNetworkDO.getPermission());
        userSemanticNetwork.setCreateTime(userSemanticNetworkDO.getCreateTime());
        userSemanticNetwork.setUpdateTime(userSemanticNetworkDO.getUpdateTime());
        return userSemanticNetwork;
    }

    private UserSemanticNetworkDO convertToDO(UserSemanticNetwork userSemanticNetwork) {
        if (userSemanticNetwork == null) {
            return null;
        }
        UserSemanticNetworkDO userSemanticNetworkDO = new UserSemanticNetworkDO();
        userSemanticNetworkDO.setId(userSemanticNetwork.getId());
        userSemanticNetworkDO.setUserId(userSemanticNetwork.getUserId());
        userSemanticNetworkDO.setSemanticNetworkId(userSemanticNetwork.getSemanticNetworkId());
        userSemanticNetworkDO.setPermission(userSemanticNetwork.getPermission());
        userSemanticNetworkDO.setCreateTime(userSemanticNetwork.getCreateTime());
        userSemanticNetworkDO.setUpdateTime(userSemanticNetwork.getUpdateTime());
        return userSemanticNetworkDO;
    }
} 